//
//  DWRACViewController.m
//  OCAPPDemo
//
//  Created by 杨冬武 on 2021/5/29.
//

#import "DWRACViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>

@interface DWRACViewController ()

@end

@implementation DWRACViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}



- (void)rac {
    
//    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
//        [subscriber sendNext:@"next"];
//        return [RACDisposable disposableWithBlock:^{
//            NSLog(@"释放");
//        }];
//    }];
//
//    [signal subscribeNext:^(id  _Nullable x) {
//        NSLog(@"接受到信号内容 %@", x);
//    } error:^(NSError * _Nullable error) {
//        NSLog(@"失败 %@", error);
//    }];
    
//    [[self rac_signalForSelector:@selector(changeFullName:)] subscribeNext:^(RACTuple * _Nullable x) {
//        NSLog(@"changeFullName %@", x);
//    }];

//    RACSignal * s =[self.person rac_valuesAndChangesForKeyPath:@"fullName" options:NSKeyValueObservingOptionNew observer:self];
//    [s subscribeNext:^(id  _Nullable x) {
//        NSArray *a = [x allObjects];
//        NSLog(@"person changeFullName %@", a);
//    }];
    
//    [[self.racButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        NSLog(@"button %@", x);
//    }];
    
//    [[self.textField rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        NSLog(@"textField %@", x);
//    }];
    
//    [self.textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
//        NSLog(@"textField %@", x);
//    }];
    
    /*
    RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@"发送请求1"];
        [subscriber sendCompleted];
        
        return nil;
    }];
    
    RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@"发送请求2"];
        [subscriber sendCompleted];
        
        return nil;
    }];
    
    // 使用注意：几个信号，参数一的方法就几个参数，每个参数对应信号发出的数据。
    [self rac_liftSelector:@selector(updateUIWithR1:r2:) withSignalsFromArray:@[request1,request2]];
    */
//    RAC(self.person, firstName) = self.textField.rac_textSignal;
    
//    [RACObserve(self.person, firstName) subscribeNext:^(id  _Nullable x) {
//        NSLog(@"RACObserve %@", x);
//        self.textField.text = x;
//    }];
//
//    @weakify(self);
//    @strongify(self);
    
    
    
}

- (void)connection {
    //RACMulticastConnection:用于当一个信号，被多次订阅时，为了保证创建信号时，避免多次调用创建信号中的block，造成副作用，可以使用这个类处理。
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"signal");
        [subscriber sendNext:@"aaa"];
        return nil;
    }];
    
    //使用RACMulticastConnection不会多次调用RACSignal的Block
    RACMulticastConnection *connect = [signal publish];
    
    [connect.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"connect.signal 01 %@", x);
    }];
    
    [connect.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"connect.signal 02 %@", x);
    }];
    
    [connect connect];
}

- (void)racCommand {
    
    //RACCommand:RAC中用于处理事件的类，可以把事件如何处理,事件中的数据如何传递，包装到这个类中，他可以很方便的监控事件的执行过程。
    
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //发送数据
            [subscriber sendNext:@"123"];
            //当命令内部发送数据完成，一定要主动发送完成
            //注意：数据传递完，最好调用sendCompleted，这时命令才执行完毕。
            [subscriber sendCompleted];
            
            
            NSLog(@"subscriber:%@======",subscriber);
            return nil;
        }];
    }];
    
//    RACSignal *s = [command execute:@"abcddd"];
//    [s subscribeNext:^(id  _Nullable x) {
//        NSLog(@"x = %@", x);
//    }];
    
//    [command.executionSignals subscribeNext:^(id  _Nullable x) {
//        NSLog(@"x = %@", x);
//    }];
    
    [command.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        NSLog(@"x = %@", x);
    }];
    //默认会走一次，没有执行，可以使用 [command.executing skip:1] 跳过一步
    [command.executing subscribeNext:^(NSNumber * _Nullable x) {
        if (x.boolValue) {
            NSLog(@"正在执行");
        } else {
            NSLog(@"执行完成/没有执行");
        }
    }];
    
    [command execute:@""];
    
}

- (IBAction)addRACSubcribe:(id)sender {
    
    [self racCommand];
}

- (void)updateUIWithR1:(id)x r2:(id)y {
    NSLog(@"%s %@, %@", __FUNCTION__, x, y);
}

@end
